﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Charting;

namespace BSCMFF.Web.Pages.Reward_System
{
    public partial class CompetencyChart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (EvalSelection.PublicSelectedValue != 0)
            {
                FillChart(EvalSelection.PublicSelectedValue);
            }
        }

        private void FillChart(int evalID)
        {
            BSCEntities context = new BSCEntities();
            Evaluation_Period ep;

            //if (EvalSelection.PublicSelectedValue == 0)
            //{
            //    return;
            //}
            //else
            //{
            //    ep = (from temp in context.Evaluation_Period
            //          where temp.ID == evalID
            //          select temp).SingleOrDefault();
            //}
            //if (ep.Reward != true)
            //{
            //    return;
            //}

            var data = (from ev in context.Evaluation_User_Question_Value
                        join ee in context.Evaluation_Evaluator_Employee on ev.EEE_FK equals ee.ID
                        where ee.Evaluation_FK == evalID
                        join eq in context.Evaluation_Question on ev.Evaluation_Question_FK equals eq.ID
                        join ec in context.Evaluation_Category on eq.Evaluation_Category_FK equals ec.ID
                        group ev by new { ee.Employee_FK } into g
                        join u in context.Users on g.Key.Employee_FK equals u.ID
                        join epp in context.Evaluation_Period on evalID equals epp.ID
                        //orderby g.Sum(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / g.Sum(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min) descending
                        select new
                        {
                            //Fullname = u.Firstname + u.Lastname,
                            perc = g.Sum(ev => ev.Value - ev.Evaluation_Question.Evaluation_Category.Min) * 100 / g.Sum(ev => ev.Evaluation_Question.Evaluation_Category.Max - ev.Evaluation_Question.Evaluation_Category.Min)

                            //salary = u.Salary == null ? 0 : (int)u.Salary,
                            //bonus = u.Salary == null ? 0 : (int)(u.Salary * epp.CompetencyCoef / 100)
                        }).ToList();


            List<int> percents = new List<int>();
            KeyValuePair<int, int> intervals = new KeyValuePair<int, int>();

            int[,] more = new int[100, 1];

            const int sizeOfInterval = 1;
            foreach (var d in data)
            {
                int interval = d.perc / sizeOfInterval;

                ++more[interval, 0];

            }

            ChartSeries chs = new ChartSeries("Perc", ChartSeriesType.Bar);
            chs.Appearance.ShowLabels = false;

            for (int i = 0; i < more.Length; ++i)
            {
                ChartSeriesItem chi = new ChartSeriesItem();
                chi.XValue = i;
                if (more[i, 0] != 0)
                {
                    chi.YValue = more[i, 0];
                }
                else
                {
                    //chi.Empty = true;
                }
                //chi.Label.TextBlock.Text = "";
                //chi.Name = "";
                chs.Items.Add(chi);
            }

            //for (int i = 0; i < chs.Items.Count; ++i)
            //{
            //    if (chs.Items[i].Empty == false)
            //    {
            //        break;
            //    }
            //    chs.Items[i].Empty = false;
            //    chs.Items[i].YValue = 0;
            //}


            RadChart1.Series.Add(chs);
            //chs.PlotArea.XAxis.AutoScale = false;
        }
    }
}